package com.handler;

import com.ymx.darling.bean.sql.SqlStatement;
import com.ymx.darling.build.mapper.MapperMethodInfoInfoManager;
import com.ymx.darling.build.mapper.method.ProviderMethodInfo;
import com.ymx.darling.dialect.page.Page;
import com.ymx.darling.session.Session;
import com.ymx.darling.wrapper.QueryWrapper;

/**
 * @author 爱Java的小于
 */
public class SelectPage extends ProviderMethodInfo {
    @Override
    public Object invoke(Class<?> mapperClass, Session session, String sqlStatementId, Object[] param) throws Throwable {
        Class<?> entityType = MapperMethodInfoInfoManager.getEntityType(mapperClass);
        QueryWrapper queryWrapper = (QueryWrapper) param[0];

        SqlStatement sqlStatement = queryWrapper.toSql(entityType);
        Page<Object> page = session.selectPage(entityType, sqlStatement.getSql(), sqlStatement.getParamList().toArray());
        PageInfo<Object> pageInfo = new PageInfo<>();
        pageInfo.setValueList(page.getList());
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setCount(page.getTotal());
        pageInfo.setNextPage(page.getCurrent() < page.getPageCount());

        return pageInfo;
    }
}
